package pe.edu.upc.qwerty.movil;

import java.util.Vector;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Lista  extends ListActivity  {
	
	//private static final String TAG = "Lista";	
	
	public static final String NAMESPACE = "http://service.wsreserva.qwerty.dsd.upc.edu.pe/";
	public static String URL = "http://64.76.72.75/wsreserva/services/vueloService?wsdl";
	private static final String METHOD_NAME = "obtenerListaVuelos";
	//private static final String SOAP_ACTION =  "http://service.wsreserva.qwerty.dsd.upc.edu.pe/obtenerListaVuelos";
	public static final int TIMEOUT = 50000;
	
	private String[] maestroVuelos;
		
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("LOAD","cargo pantalla lista");
       
        try {        	
        	cargaSimple();	
		} catch (Exception e) {
			e.printStackTrace();
			Log.d("ERR",e.getMessage());			
		}
		
		 ListView lv = getListView();
	     lv.setTextFilterEnabled(true);
        
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener  () {
	          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		           Log.d("LISTA", "Seleccionado = " + position);
		           
		           //TextView txt =(TextView)parent.getChildAt( position ).findViewById( R.id.list );
		           
		           String keyword = maestroVuelos[position].split(",")[0];
		          
	               Intent intent = new Intent(Lista.this, Vuelo.class);
			        Bundle bundle = new Bundle();
			        bundle.putString("codVuelo", keyword );			      
	             intent.putExtras(bundle);
	             startActivity(intent);
		           
	          }
        });
        
	}
	
	public void cargaSimple(){		
		maestroVuelos =listaVuelos();//new String[] {"AA-522","TA-666","LF-963"};	//
		
		String[] oba = new String[maestroVuelos.length];
		int i=0;
		for (String cadena : maestroVuelos) {
			String[] splCadena = cadena.split(",");
			oba[i] = splCadena[1];
			i++;
		}
		setListAdapter(new ArrayAdapter<String>(this, R.layout.lista, oba ));		 
	}
	
	

    
    @SuppressWarnings("unchecked")
	public String[] listaVuelos(){
    	String[] res= null;
    	 try {	     		
     			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);              
     			  
     			// enviando objetos simples como parametros del metodo
	            //request.addProperty("", txtNom.getText().toString());
     			
     			//enviando objetos complejos como parametros del metodo
	            /*
	            SoapObject vuelos = new SoapObject(NAMESPACE, "alertasRequest");
	            vuelos.addProperty("apeMaterno", "txtApMaterno.getText().toString()");
	            PropertyInfo documentIdsPropertyInfo = new PropertyInfo();
	            documentIdsPropertyInfo.setName("obtenerListaVuelos");
	            documentIdsPropertyInfo.setValue(vuelos);
	            documentIdsPropertyInfo.setType(vuelos.getClass());
	           	request.addProperty( documentIdsPropertyInfo );
	            */
     					
	            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
	            envelope.setOutputSoapObject(request);
	            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,TIMEOUT);	           
	         	androidHttpTransport.call( NAMESPACE+METHOD_NAME , envelope);	         	
	         	Vector<SoapObject> v = (Vector<SoapObject>)envelope.getResponse();	         	
	        	res = new String[ v.size() ];
	        	int i = 0 ; 
	         	for(SoapObject o : v){
	         		res[i++] = o.getPropertyAsString("idVuelo")+","+o.getPropertyAsString("codigoVuelo") ;
	         	}	         	
     		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
    }
    

}
